package cn.ac.iscas.action.softwareCopyright;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import cn.ac.iscas.bean.project.Project;
import cn.ac.iscas.bean.softwareCopyright.SoftwareCopyright;
import cn.ac.iscas.bean.softwareCopyright.SoftwarePicture;
import cn.ac.iscas.service.project.IProjectService;
import cn.ac.iscas.service.softwareCopyright.ISoftwareCopyrightService;
import cn.ac.iscas.service.softwareCopyright.ISoftwarePictureService;

@Controller @Scope("prototype")
public class SoftwareCopyrightManageAction extends ActionSupport{
	/**
	 *处理项目的录入，保存，修改，删除的功能
	 */
//	private static final long serialVersionUID = 2997402046169380022L;
//	@Resource private IProjectService projectService;//运行时动态注入projectService层
//	@Resource private IProjectDocumentService projectDocumentService;//运行时动态注入projectDocumentService层
	@Resource private ISoftwareCopyrightService softwareCopyrightService;
	@Resource private ISoftwarePictureService softwarePictureService;
	@Resource private IProjectService projectService;//运行时动态注入projectService层
	private Project project;
//	private Project project;//接收处理时动态注入
//	private ProjectDocument document;//接收处理时动态注入
	private SoftwareCopyright softwareCopyright;
	private SoftwarePicture softwarePicture;	
	
	/**
	 *上传时动态接收注入的文档文件
	 **/
	private File[] applyingDocument;
	
	/**
	 *上传时动态接收注入的文档文件类型
	 **/
	private String[] applyingDocumentContentType;
	
	/**
	 *上传时动态接收注入的文档文件名
	 **/
	private String[] applyingDocumentFileName;
	
	/**
	 * 处理项目的详细查询
	 * */
	public String detailQuery()//详细信息查询
	{
		softwareCopyright = softwareCopyrightService.get(SoftwareCopyright.class, softwareCopyright.getId());
		return "detailQuery";//跳转到详细信息界面
	}
	/**
	 * 处理项目的修改
	 * */
	public String edit()//项目信息修改
	{
		softwareCopyright = softwareCopyrightService.get(SoftwareCopyright.class, softwareCopyright.getId());
		return "edit";//跳转修改界面
	}
	/**
	 * 处理项目的更新
	 * */
	public String update() throws IOException
	{
		//得到原有项目文档
		Set<SoftwarePicture> pictures = softwareCopyrightService.get(SoftwareCopyright.class, softwareCopyright.getId()).getPicturePaths();

		for(SoftwarePicture pic:pictures)//更新原有的项目文档
		{
			pic.setSoftwareCopyright(softwareCopyright);
		}
		
		String realpath = ServletActionContext.getServletContext().getRealPath("/documents");
		
		Set<SoftwarePicture> softwareCopyrightPictures = new HashSet<SoftwarePicture>();
		
		
		//处理申请的文档更新
		if(applyingDocument != null)
		{
			File savedir = new File(realpath);
			if(!savedir.exists()) savedir.mkdir();
			for(int i=0; i<applyingDocument.length;i++)
			{
				SoftwarePicture picInfo = new SoftwarePicture();
				String realName = UUID.randomUUID().toString()+ getExt(applyingDocumentFileName[i]);
				File savefile = new File(savedir , realName);
				FileUtils.copyFile(applyingDocument[i], savefile);
				picInfo.setPictureName(applyingDocumentFileName[i]);
				picInfo.setRealName(realName);
				picInfo.setPath(realpath);
				picInfo.setUploadTime(new Date());
				picInfo.setSoftwareCopyright(softwareCopyright);
				softwareCopyrightPictures.add(picInfo);
			}
		}
		
		//将项目更新导数据库中
		if(project.getId() != 0)
		{
			project = projectService.get(Project.class, project.getId());
		}
		else
		{
			Project oldProject = softwareCopyrightService.get(SoftwareCopyright.class, softwareCopyright.getId()).getProject();
			project = oldProject;
		}
		softwareCopyright.setProject(project);
		softwareCopyrightService.update(softwareCopyright);
		for(SoftwarePicture pic:pictures)
		{
			softwarePictureService.save(pic);//保存原有的项目文档
		}
		
		//保存项目文档  注意必须先保存文档再保存项目
		for(SoftwarePicture pic:softwareCopyrightPictures)
		{
			softwarePictureService.save(pic);
		}
		return "success";
	}
	/**
	 * 
	 * 项目保存
	 * */
	public String save() throws IOException
	{
		if(project!=null)
		{
		  project = projectService.get(Project.class, project.getId());
		}
		else
			System.err.println("erro");
		String realpath = ServletActionContext.getServletContext().getRealPath("/documents");
		Set<SoftwarePicture> softwarePictures = new HashSet<SoftwarePicture>();
		if(applyingDocument != null)
		{
			File savedir = new File(realpath);
			if(!savedir.exists()) savedir.mkdir();
			for(int i=0; i<applyingDocument.length;i++)
			{
				SoftwarePicture picInfo = new SoftwarePicture();
				String realName = UUID.randomUUID().toString()+ getExt(applyingDocumentFileName[i]);
				File savefile = new File(savedir , realName);
				FileUtils.copyFile(applyingDocument[i], savefile);
				picInfo.setPictureName(applyingDocumentFileName[i]);
				picInfo.setRealName(realName);
				picInfo.setPath(realpath);
				picInfo.setUploadTime(new Date());
				picInfo.setSoftwareCopyright(softwareCopyright);	
				softwarePictures.add(picInfo);
			}
			System.out.println("上传软著图片成功");
			System.out.println(realpath);
		}
		softwareCopyright.setProject(project);
		softwareCopyrightService.save(softwareCopyright);
		for(SoftwarePicture pic:softwarePictures)
		{
			try{
				softwarePictureService.save(pic);
			}catch(Exception ex){
				ex.printStackTrace();
			}
			System.out.println("保存软著picture成功");
		}
		
		System.out.println("*****************************************");
		
		return "success";
	}
	/**
	 * 
	 *删除项目文档Ajax
	 * */
	public String deleteDocument() throws IOException
	{
		softwarePicture = softwarePictureService.get(SoftwarePicture.class, softwarePicture.getId());
		softwarePictureService.delete(softwarePicture);
		ActionContext ctx = ActionContext.getContext();     
	    HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
	    response.setContentType("text/html;charset=utf-8");
	    PrintWriter out=response.getWriter();
	    response.setContentType("text/html;charset=utf-8");
	    out.println("成功删除");
		return null;
	}
	/**
	 * 
	 *删除项目Ajax
	 * */
	public String delete() throws IOException
	{
		softwareCopyright = softwareCopyrightService.get(SoftwareCopyright.class,softwareCopyright.getId());
		softwareCopyrightService.delete(softwareCopyright);
		ActionContext ctx = ActionContext.getContext();     
	    HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
	    response.setContentType("text/html;charset=utf-8");
	    PrintWriter out=response.getWriter();
	    response.setContentType("text/html;charset=utf-8");
	    out.println("成功删除");
		return null;
	}
	
	
	public String[] getApplyingDocumentContentType() {
		return applyingDocumentContentType;
	}
	public void setApplyingDocumentContentType(String[] applyingDocumentContentType) {
		this.applyingDocumentContentType = applyingDocumentContentType;
	}
	public static String getExt(String fileName)
	{
		return fileName.substring(fileName.lastIndexOf("."));
	}
	public File[] getApplyingDocument() {
		return applyingDocument;
	}
	public void setApplyingDocument(File[] applyingDocument) {
		this.applyingDocument = applyingDocument;
	}
	public String[] getApplyingDocumentFileName() {
		return applyingDocumentFileName;
	}
	public void setApplyingDocumentFileName(String[] applyingDocumentFileName) {
		this.applyingDocumentFileName = applyingDocumentFileName;
	}
	
	
	
	public ISoftwareCopyrightService getSoftwareCopyrightService() {
		return softwareCopyrightService;
	}
	public void setSoftwareCopyrightService(
			ISoftwareCopyrightService softwareCopyrightService) {
		this.softwareCopyrightService = softwareCopyrightService;
	}
	public SoftwareCopyright getSoftwareCopyright() {
		return softwareCopyright;
	}
	public void setSoftwareCopyright(SoftwareCopyright softwareCopyright) {
		this.softwareCopyright = softwareCopyright;
	}
	public SoftwarePicture getSoftwarePicture() {
		return softwarePicture;
	}
	public void setSoftwarePicture(SoftwarePicture softwarePicture) {
		this.softwarePicture = softwarePicture;
	}
	public void setProjectService(IProjectService projectService) {
		this.projectService = projectService;
	}
	public IProjectService getProjectService() {
		return projectService;
	}
	public void setProject(Project project) {
		this.project = project;
	}
	public Project getProject() {
		return project;
	}
}
